package com.hhit.collaborative_learning.management;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.utils.DBAccess;
import com.mysql.jdbc.StringUtils;

public class SelectUserNameServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username=request.getParameter("username");
		String nickname=request.getParameter("nickname");
		Connection conn=DBAccess.getInstance().getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="";
		if(!StringUtils.isNullOrEmpty(username)){
			sql+="select count(*) from user where userName='"+username+"'";
		}else if(!StringUtils.isNullOrEmpty(nickname)){
			sql+="select count(*) from user where userNickName='"+nickname+"'";
		}
		int count=0;
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		if(count==0){
			response.getWriter().print("true");
		}else{
			response.getWriter().print("false");
		}
	}

}
